var BASE_URL='http://192.168.10.11/wnsgbl-repo/mobileapp/';
var BASE_URL_IMAGE='http://192.168.10.11/wnsgbl-repo/static/uploads/';
//var BASE_URL='http://winsglobal.com/mobileapp/';
//var BASE_URL_IMAGE='http://winsglobal.com/static/uploads/';

/* GET TRAVEL DATA */
var travelData = function() {
    var opts = {
		type:"POST",
/*		beforeSend:function(){
			$("#progressbar").show();
		},*/
		success:function(data){
			var html = "";
			for (var i = 0; i < data.length; i++) {
				var id = data[i].postID;
				var title = data[i].postTitle;
				var image = data[i].imageName;
				var video = data[i].videoLink;
				var desc = data[i].body;
                 var uid = 'detail'+id;

				html += '<li><a onclick=detailPage("'+id+'") href="#'+uid+'"><div class="trav_list_title">'+title +'</div></a></li>';
				
				if(image){
					var img = "<img class='list_image' src="+image+">";
				}else{
					var img = "";
				}			
				
				//var panel_content = '<h2>'+title+'</h2><figure class="video_format">'+video+'</figure>'+img+'<p>'+desc+'</p>';
                //$.ui.addContentDiv(id, panel_content, "Travel Detail");
			}
			/*$("#progressbar").hide();*/
			$('#show_travel_data').append(html);
			
		},
		error: function (xhr, ajaxOptions, thrownError) {
            alert(xhr.status);
            alert(thrownError);
        },
		url:BASE_URL+'get_travel_paging/1/null'
	}
	$.ajax(opts);
};

function detailPage(itemID)
{
    $.ajax({
        type:"POST",
        data:{'postid':itemID},
        success:function(data){
            var list_html = "";
            var id = data[0].postID;
            var title = data[0].postTitle;
            var image = data[0].imageName;
            var video = data[0].videoLink;
            var desc = data[0].body;

            if(image){
                var img = "<img class='list_image' src="+image+">";
            }else{
                var img = "";
            } 

            list_html += '<h2>'+title+'</h2><figure class="video_format">'+video+'</figure>'+img+'<p>'+desc+'</p>';

            $.ui.updateContentDiv("#page_detail",'<div>'+list_html+'</div>');       
            
            if(list_html){
                $.ui.loadContent("#page_detail",false,false,"slide");
            }
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert(xhr.status);
            alert(thrownError);
        },
        url:BASE_URL+'get_travel_detail'
    });
}
//detailPage();

// enable pullToRefresh and infiniteScroll to listview (af.scroller.js)
var currentPage = 1;
function setupListUpdate(){
    $.ui.ready(function () {
        var scrollerList = $("#main").scroller();
        scrollerList.addPullToRefresh();
        $.bind(scrollerList, "refresh-release", function () {
            var self = this;
            currentPage = 1;
            $.ajax({
                type:"POST",
               	url:BASE_URL+'get_travel_paging/1/null',
                success:function(data){
                    var list_html = "";
                    for(var i=0; i< data.length; i++){
                        var id = data[i].postID;
						var title = data[i].postTitle;
						var image = data[i].imageName;
						var video = data[i].videoLink;
						var desc = data[i].body;
                        list_html += '<li><a onclick=detailPage("'+id+'") href="#'+id+'">'+ title +'</a></li>';
                    }
                    $("#show_travel_data").html(list_html);
                    self.hideRefresh();
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    self.hideRefresh();
                    alert(xhr.status);
                    alert(thrownError);
                }
            });
            return false; //tells it to not auto-cancel the refresh
        });

        scrollerList.addInfinite();
        $.bind(scrollerList, "infinite-scroll", function () {
            var self = this;
            
            $("#main").find("#infinite").text("Loading...");
            /*setTimeout(function () { // get content from your api using ajax and display instead of setTimeout.
                $("#catalogueList").find("#infinite").text("Load More");
                // add new content at bottom of list
                $("#catalogueList ul").append("<li><a href='#detailview'>Next Item (via Infinite Scroll)</a></li>");
                self.clearInfinite();
            }, 2000);*/
            //getCatalogues(true);
            var goToPage = currentPage+1;
            $.ajax({
                type:"GET",
                url:BASE_URL+"get_travel_paging/"+goToPage+"/null",
                //data:{'foo':'bar'},
                success:function(data){
                    var list_html = "";
                    if(data && data.length > 0) {
                        for(var i=0; i< data.length; i++){
                            var id = data[i].postID;
                            var title = data[i].postTitle;
                            var image = data[i].imageName;
                            var video = data[i].videoLink;
                            var desc = data[i].body;
                            list_html += '<li><a onclick=detailPage("'+id+'") href="#'+id+'">'+ title +'</a></li>';
                        }
                        $("#show_travel_data").append(list_html);
                        currentPage = currentPage + 1;
                    }
                    //alert('test '+currentPage);
                    self.clearInfinite();
                    $("#main").find("#infinite").text("Load More");
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    self.clearInfinite();
                    alert(xhr.status);
                    alert(thrownError);
                }
            });
            
        });

        scrollerList.enable();
    });
}
